package com.mgm.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.mgm.affsettings.service.AffsettingsService;
import com.mgm.conversion.service.ConversionService;
import com.mgm.dto.AffsettingsDto;
import com.mgm.dto.ConsumeCapDto;
import com.mgm.exception.CustomException;
import com.mgm.page.DataGrid;
import com.mgm.result.IMessage;
import com.mgm.result.IResult;

@Controller
@RequestMapping("/affsettings")
public class AffsettingsController {
	
	private static Logger logger =LoggerFactory.getLogger(AffsettingsController.class);
	
	@Autowired
	private AffsettingsService affsettingsService;
	@Autowired
	private ConversionService conversionService;
	
	/**
	 * 
	 * @Title: listAdvertisers   
	 * @Description: 渠道offer设置列表  
	 * @param: @param pageNum
	 * @param: @param pageSize
	 * @param: @param advId
	 * @param: @param advName
	 * @param: @param pkgname
	 * @param: @param countries
	 * @param: @param enabled
	 * @param: @param adffId
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IResult<DataGrid>>      
	 * @throws
	 */
	@RequestMapping(value = "/{affId}/list", method = RequestMethod.GET)
	public ResponseEntity<IResult<DataGrid>> listAdvertisers(@RequestParam(value="page", defaultValue="1") Integer pageNum, @RequestParam(value="ps", defaultValue="10") Integer pageSize, 
			@RequestParam(value="adv_id",required=false) String advId, @RequestParam(value="adv_name",required=false) String advName,
			@RequestParam(value="pkgname",required=false) String pkgname, @RequestParam(value="countries",required=false) String countries,
			@RequestParam(value="enabled",required=false) Boolean enabled, @PathVariable("affId") Integer adffId, 
			HttpServletRequest request, HttpServletResponse response) {
		AffsettingsDto affsettingsDto = new AffsettingsDto();
		affsettingsDto.setAdffId(adffId); //必传
		affsettingsDto.setAdv_id(advId); //广告主编码
		affsettingsDto.setEnabled(enabled);
		affsettingsDto.setAdv_name(advName);
		affsettingsDto.setCountries(countries);
		
		DataGrid dataGrid = affsettingsService.findAll(affsettingsDto, pageNum, pageSize);
		IResult<DataGrid> result = IResult.success(dataGrid);
		
		return new ResponseEntity<IResult<DataGrid>>(result, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: premium   
	 * @Description: 修改渠道溢价
	 * @param: @param affId
	 * @param: @param map
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(value = "/{affId}/premium", method = RequestMethod.PUT) 
	public ResponseEntity<IMessage> premium(@PathVariable("affId") Integer affId, 
			@RequestBody Map<String, String> map, 
			HttpServletRequest request, HttpServletResponse response) {
		String advId = map.get("advmentids"); //广告Id
		String premium = map.get("premium"); //溢价
		
		if (StringUtils.isEmpty(advId) || StringUtils.isEmpty(premium)) {
			return new ResponseEntity<IMessage>(IMessage.error("广告ID或溢价不能为空"), HttpStatus.OK);
		}
		String[] admentids = advId.split(",");
		List<Integer> admentIdList = new ArrayList<>();
		for (String string : admentids) {
			admentIdList.add(Integer.valueOf(string));
		}
		IMessage msg = affsettingsService.modifyPremiumBatch(affId, admentIdList, Integer.valueOf(premium));
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: restorePremium   
	 * @Description: 还原溢价 
	 * @param: @param affId
	 * @param: @param map
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(value = "/{affId}/premium/clean", method = RequestMethod.PUT) 
	public ResponseEntity<IMessage> restorePremium(@PathVariable("affId") Integer affId, 
			@RequestBody Map<String, String> map, 
			HttpServletRequest request, HttpServletResponse response) {
		String advId = map.get("advmentids"); //广告Id
		
		if (StringUtils.isEmpty(advId)) {
			return new ResponseEntity<IMessage>(IMessage.error("非法参数！"), HttpStatus.OK);
		}
		String[] admentids = advId.split(",");
		List<Integer> admentIdList = new ArrayList<>();
		for (String string : admentids) {
			admentIdList.add(Integer.valueOf(string));
		}
		Integer premium = null;//map.get("premium"); //溢价(默认=置空)
		
		IMessage msg = affsettingsService.modifyPremiumBatch(affId, admentIdList, premium);
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: deleteAdvertisement   
	 * @Description: 删除
	 * @param: @param affId
	 * @param: @param ad_id
	 * @param: @param admentId
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(value = "/{affId}/advertisement/{admentId}/advertiser/{adId}", method = RequestMethod.DELETE) 
	public ResponseEntity<IMessage> deleteAdvertisement(@PathVariable("affId") Integer affId,
			@PathVariable("adId") Integer adId, @PathVariable("admentId") Integer admentId, 
			HttpServletRequest request, HttpServletResponse response) {

		IMessage msg= affsettingsService.deleteByKeys(affId, admentId, adId);
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: enabled   
	 * @Description: 渠道下发开关
	 * @param: @param affId
	 * @param: @param map
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(value = "/{affId}/enabled", method = RequestMethod.PUT) 
	public ResponseEntity<IMessage> enabled(@PathVariable("affId") Integer affId, 
			@RequestBody Map<String, String> map, 
			HttpServletRequest request, HttpServletResponse response) {
		String advId = map.get("advmentids"); //广告Id
		String enabled = map.get("enabled"); //开关
		 
		if(StringUtils.isEmpty(advId) || StringUtils.isEmpty(enabled)) {
			logger.error("[Enabled Advertisement] Illegal operation");
			throw new CustomException("非法操作");
		}
		
		String[] admentids = advId.split(",");
		List<Integer> admentIdList = new ArrayList<>();
		for (String string : admentids) {
			admentIdList.add(Integer.valueOf(string));
		}
		IMessage msg= affsettingsService.updateEnabledBatch(affId, admentIdList, Boolean.valueOf(enabled));
		
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: updateCap   
	 * @Description: 修改广告对渠道cap
	 * @param: @param affId
	 * @param: @param admentId
	 * @param: @param map
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(value="/{affId}/advertisement/{admentId}/cap", method = RequestMethod.PUT) 
	public ResponseEntity<IMessage> updateCap(@PathVariable("affId") Integer affId, 
			@PathVariable("admentId") Integer admentId, @RequestBody Map<String, BigDecimal> map, 
			HttpServletRequest request, HttpServletResponse response){
		 
		IMessage msg= affsettingsService.modifyCaps(affId, admentId, map.get("total"), map.get("daily"));
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}
	
	/**
	 * 
	 * @Title: restoreCap   
	 * @Description: 还原广告对渠道溢价
	 * @param: @param affId
	 * @param: @param admentId
	 * @param: @param map
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<IMessage>      
	 * @throws
	 */
	@RequestMapping(value="/{affId}/advertisement/{admentId}/cap/clean", method = RequestMethod.PUT) 
	public ResponseEntity<IMessage> restoreCap(@PathVariable("affId") Integer affId, 
			@PathVariable("admentId") Integer admentId,
			HttpServletRequest request, HttpServletResponse response){
		 
		IMessage msg= affsettingsService.modifyCaps(affId, admentId, null, null); //map.get("total"), map.get("daily")
		return new ResponseEntity<IMessage>(msg, HttpStatus.OK);
	}

	/**
	 * 渠道Offer消耗Cap
	 * @Title: getCap   
	 * @Description: 总cap/日cap   
	 * @param: @param affCode
	 * @param: @param offerid
	 * @param: @param request
	 * @param: @param response
	 * @param: @return      
	 * @return: ResponseEntity<ConsumeCapDto>      
	 * @throws
	 */
	@RequestMapping(value = "/{affCode}/{offerid}/consumecap", method = RequestMethod.GET)
	public ResponseEntity<ConsumeCapDto> getCap(
			@PathVariable("affCode") String affCode, @PathVariable("offerid") String offerid,
			HttpServletRequest request, HttpServletResponse response) {
		ConsumeCapDto comCapDto = new ConsumeCapDto();
		comCapDto.setConsume_payout_cap(conversionService.selectTotalCapByAffId(offerid, affCode));
		comCapDto.setConsume_daily_payout_cap(conversionService.selectTotalCapByAffId(offerid, affCode));
		return new ResponseEntity<ConsumeCapDto>(comCapDto, HttpStatus.OK);
	}
}
